/**
 * 
 */
package com.liupf.controller;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.liupf.common.utils.MD5Util;

/**
 * @author shevliu
 *
 */
public class MyController extends Controller {

	private Log log = LogFactory.getLog(this.getClass());

	public void index() {
		render("../index.ftl");
	}
	
	public void editPWD(){
		render("password_edit.ftl");
	}
	
	public void updatePWD(){
		String oldPWD = MD5Util.MD5(getPara("oldPWD"));
		String newPWD = getPara("newPWD");
		String newPWD2 = getPara("newPWD2");

		int userId = NumberUtils.toInt(getSession().getAttribute("userId") + "");
		String querySQL = "select password from sys_user where id = ?" ;
		String dbPWD = Db.findFirst(querySQL , userId).getStr("password");
		if(!oldPWD.equals(dbPWD)){
			renderError500();
		}
		else if(!newPWD.equals(newPWD2)){
			renderError500();
		}
		else{
			Db.update("update sys_user set password = ? where id = ?" , MD5Util.MD5(newPWD) , userId);
			redirect("/logout");
		}
	}

}
